﻿using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;
using com.fukefu;
using com.fukefu.source;
using com.fukefu.sql;

namespace CSDN赚分机
{
    public partial class MainForm : BaseForm
    {
        AccessOperate access = new AccessOperate("data.mdb");
        Source source = new Source();
        Random random = new Random();

        int MaxId = 0, sleepTime = 2;
        public MainForm()
        {
            InitializeComponent();
        }

        private void ZF()
        {
            RegeditOperate reg = new RegeditOperate("csdn");
            string name = reg.GetString("username");
            string pass = reg.GetString("password");
            string code = InputBox.ShowInputBox("http://passport.csdn.net/ajax/verifyhandler.ashx");

            ParametersData pd = new ParametersData();
            pd.Add("t", "log");
            pd.Add("u", name);
            pd.Add("p", pass);
            pd.Add("c", code);
            pd.Add("remember", "0");
            pd.Add("f", "http://passport.csdn.net/account/login");
            pd.Add("rand", "0.3313852854700503");

            string html = source.Get("http://passport.csdn.net/ajax/accounthandler.ashx?" + pd.Read());
            if (html.IndexOf("status\":true,\"error\":\"\",\"data\":") != -1)
            {
                SetControl("listbox1", ControlValue.ListBox, name + " 登陆成功");
                MaxId = access.GetQueryNum("select max(csdnid) from [资源]") + 1;
                if (MaxId < 200000)
                {
                    MaxId += 200000;
                }
                //启动评分
                Thread t = new Thread(new ThreadStart(PF));
                t.IsBackground = true;
                t.Start();
                while (true)
                {
                    string content = source.Get("http://download.csdn.net/download/1/" + MaxId);
                    if (content.IndexOf("title=\"电信下载\"") != -1 && content.IndexOf("资源积分：0分") != -1)
                    {
                        string url = Regex.Match(content, "method=\"post\" action=\"([^\"]+)\"").Groups[1].Value;
                        Stream s = source.PostStream(url, "ds=dx", "http://download.csdn.net/download/" + Regex.Match(url, "do_download/" + MaxId + "/([^/]+)/").Groups[1].Value + "/" + MaxId);
                        if (s != null)
                        {
                            s.Close();
                            access.Update("insert into 资源(csdnid) values(" + MaxId + ")");
                            SetControl("listbox1", ControlValue.ListBox, "采集资源:" + MaxId + " √");
                        }
                        else
                        {
                            SetControl("listbox1", ControlValue.ListBox, "采集资源:" + MaxId + " ×");
                        }
                    }
                    MaxId++;
                    MyFunction.Sleep(4);
                }
            }
            else if (html.IndexOf("用户名或密码错误") != -1)
            {
                SetControl("listbox1", ControlValue.ListBox, "用户名或密码错误,登陆失败");
            }
            SetControl("listview1", ControlValue.ListView, "结束了..");
            SetControl("button1", ControlValue.Enable, true);
        }
        private void PF()
        {
            int id = 0;
            string html = "";
            while (true)
            {
                id = access.GetQueryNum("SELECT Min(csdnid) FROM 资源 WHERE (((资源.[comment])=0) AND ((DateDiff('n',[dt],Date()+Time()))>15));");
                if (id != 0)
                {
                    string bt = MyFunction.GetTimestampNow().ToString();
                    Thread.Sleep(4000 + random.Next(4000));
                    string et = MyFunction.GetTimestampNow().ToString(); ;
                    html = source.Get("http://download.csdn.net/index.php/comment/post_comment?jsonpcallback=jsonp" + bt + "&sourceid=" + id + "&content=&rating=" + (random.Next(4) + 1) + "&t=" + et);
                    if (html.IndexOf("succ") != -1)
                    {
                        SetControl("listbox1", ControlValue.ListBox, "资源 " + id + " 赚取一分" + " √");
                        access.Update("update [资源] set comment=1 where csdnid=" + id);
                    }
                    else
                    {
                        SetControl("listbox1", ControlValue.ListBox, "资源 " + id + " 赚取一分" + " ×");
                    }
                    if (sleepTime != 2)
                    {
                        sleepTime -= 2;
                    }
                }
                else
                {
                    sleepTime += 2;
                }
                MyFunction.Sleep(sleepTime);
            }
        }

        private void SetButton_Click(object sender, EventArgs e)
        {
            new ConfigForm().Show();
        }

        private void BeginButton_Click(object sender, EventArgs e)
        {
            this.BeginButton.Enabled = false;
            Thread t = new Thread(new ThreadStart(ZF));
            t.IsBackground = true;
            t.Start();
        }
    }
}
